package com.zl.tby.core.base.util;
import com.alibaba.excel.EasyExcel;
import org.noear.solon.core.handle.Context;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
/**
 * @author changpeng
 * @since 2025/11/26 8:55
 */
public class EasyExcelUtils {

    /**
     * 导出Excel
     * @param ctx：上下文对象
     * @param dataList：数据
     * @param clazz: 类
     * @param fileName：文件名
     * @param sheetName: sheetName
     * @param <T>
     * @throws IOException
     */
    public static <T> void exportExcel(Context ctx,
                                       List<T> dataList,
                                       Class<T> clazz,
                                       String fileName,
                                       String sheetName) throws IOException {
        ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        ctx.charset("utf-8");
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        ctx.headerSet("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
        EasyExcel.write(ctx.outputStream(), clazz)
                .sheet(sheetName)
                .doWrite(dataList);
    }
}
